<?php

class UserPositionLevelController extends AController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='/layouts/column7';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
			//'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'.'CheckBrand'),
				'users'=>array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new UserPositionLevel;
		//获取所有职位描述
		$positionLevelsL = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "L%"');
		$positionLevelOptionL[0] = '请选择';
		foreach ($positionLevelsL as $value) {
			$positionLevelOptionL[$value['id']] = $value['name'];
		}
		$positionLevelsM = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "M%"');
		$positionLevelOptionM[0] = '请选择';
		foreach ($positionLevelsM as $value) {
			$positionLevelOptionM[$value['id']] = $value['name'];
		}
		$pa =  HrPositionAttribute::model()->findAll();
		$paOption[0] = '请选择';
		foreach ($pa as $key => $value) {
			$paOption[$value['id']] = $value['title'];
		}

		$this->render('create',array(
			'model'=>$model,'positionLevelOptionL'=>$positionLevelOptionL,'positionLevelOptionM'=>$positionLevelOptionM,'paOption' => $paOption
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST))
		{
			/*
			$userLevelModel = new UserPositionLevel;
			$userLevel = UserPositionLevel::model()->findByAttributes(array('unique_id' => $_POST['unique_id']));
			$error = 0;
			if($userLevel)
			{
				$userLevel->B = $_POST['B'];
				$userLevel->attr_id = $_POST['attr_id'];
				$userLevel->attr_M_id = $_POST['attr_M_id'];
				$userLevel->attr_L_id = $_POST['attr_L_id'];
				$userLevel->save();
			}
			*/

		}
		$userLevel = UserPositionLevel::model()->findByAttributes(array('id' => $_GET['id']));
		//获取所有职位描述
		$positionLevelsL = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "L%"');
		$positionLevelOptionL[0] = '请选择';
		foreach ($positionLevelsL as $value) {
			$positionLevelOptionL[$value['id']] = $value['name'];
		}
		$positionLevelsM = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "M%"');
		$positionLevelOptionM[0] = '请选择';
		foreach ($positionLevelsM as $value) {
			$positionLevelOptionM[$value['id']] = $value['name'];
		}
		$pa =  HrPositionAttribute::model()->findAll();
		$paOption[0] = '请选择';
		foreach ($pa as $key => $value) {
			$paOption[$value['id']] = $value['title'];
		}

		$this->render('update',array(
			'model'=>$model,'userLevel'=>$userLevel,'positionLevelOptionL'=>$positionLevelOptionL,'positionLevelOptionM'=>$positionLevelOptionM,'paOption' => $paOption
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	public function actionDetail($id)
	{
		$model=$this->loadModel($id);
		$userLevel = UserPositionLevel::model()->findByAttributes(array('id' => $_GET['id']));
		//获取所有职位描述
		$positionLevelsL = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "L%"');
		$positionLevelOptionL[0] = '请选择';
		foreach ($positionLevelsL as $value) {
			$positionLevelOptionL[$value['id']] = $value['name'];
		}
		$positionLevelsM = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "M%"');
		$positionLevelOptionM[0] = '请选择';
		foreach ($positionLevelsM as $value) {
			$positionLevelOptionM[$value['id']] = $value['name'];
		}
		$pa =  HrPositionAttribute::model()->findAll();
		$paOption[0] = '请选择';
		foreach ($pa as $key => $value) {
			$paOption[$value['id']] = $value['title'];
		}

		$this->render('detail',array(
			'model'=>$model,'userLevel'=>$userLevel,'positionLevelOptionL'=>$positionLevelOptionL,'positionLevelOptionM'=>$positionLevelOptionM,'paOption' => $paOption
		));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$condition = " status=1 ";
		if(isset($_GET['search']))
		{
			
			if($_GET['search']['s_username'])
			{
				$user = HecUser::model()->findAllByAttributes(array('status'=>'1'), " (username LIKE '%".urldecode($_GET['search']['s_username'])."%' OR c_username LIKE '%".urldecode($_GET['search']['s_username'])."%' OR unique_id LIKE '%".urldecode($_GET['search']['s_username'])."%' ) ");

				if($user)
				{
					$c = "";
					foreach ($user as $value) {
						$c .= "'".$value->unique_id."',";
					}
					$condition .= " AND unique_id IN( ".substr($c,0,-1)." ) ";
				}
			}
			if($_GET['search']['s_department'])
			{
				$user = HecUser::model()->findAllByAttributes(array('department'=>$_GET['search']['s_department']));
				if($user)
				{
					$c = "";
					foreach ($user as $value) {
						$c .= "'".$value->unique_id."',";
					}
					$condition .= " AND unique_id IN( ".substr($c,0,-1)." ) ";
				}
			}
			if($_GET['search']['attr_id'])
			{
				$condition .= "AND attr_id = '".$_GET['search']['attr_id']."' ";
			}
			if($_GET['search']['L'])
			{
				$condition .= "AND attr_L_id = '".$_GET['search']['L']."' ";
			}
			if($_GET['search']['M'])
			{
				$condition .= "AND attr_M_id = '".$_GET['search']['M']."' ";
			}
			if($_GET['search']['B'])
			{
				$condition .= "AND B = '".$_GET['search']['B']."' ";
			}
		}
		//echo $condition;
		$serch = isset($_GET['search'])?$_GET['search']:array();
		//部门
		$userDepartmentModel = new UserDepartment;
		$departmentOption = $userDepartmentModel->getOptions();
		//职位描述
		$pa =  HrPositionAttribute::model()->findAll();
		$paOption[0] = '请选择';
		foreach ($pa as $key => $value) {
			$paOption[$value['id']] = $value['title'];
		}
		//L
		$positionLevelsL = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "L%"');
		$positionLevelOptionL[0] = '请选择';
		foreach ($positionLevelsL as $value) {
			$positionLevelOptionL[$value['id']] = $value['name'];
		}
		//M
		$positionLevelsM = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "M%"');
		$positionLevelOptionM[0] = '请选择';
		foreach ($positionLevelsM as $value) {
			$positionLevelOptionM[$value['id']] = $value['name'];
		}

		$pageSize = ($_GET['search']['type'] == 'csv') ? 10000 : 30;

		//B
		$dataProvider = new CActiveDataProvider('UserPositionLevel',array(
			'criteria'=>array(
				'order' => 'id DESC',
				'condition' => $condition
				),
			'pagination'=>array(
		        'pageSize'=>$pageSize,
		    	),
			)
		); 

		if($_GET['search']['type'] == 'csv')
		{
			$arr = $dataProvider->getData() ;
			/*
			foreach ($arr as $key => $value)
			{
				
			}
			*/
			Csv::exportCsv(Yii::app()->params['userLevelFields'],$arr);
			die;
			//Yii::app()->end();
		}


		$this->render('index',array(
			'dataProvider'=>$dataProvider,'departmentOption'=>$departmentOption,'paOption' => $paOption,'positionLevelOptionL'=>$positionLevelOptionL,'positionLevelOptionM'=>$positionLevelOptionM,'search'=>$_GET['search']
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new UserPositionLevel('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['UserPositionLevel']))
			$model->attributes=$_GET['UserPositionLevel'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=UserPositionLevel::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-position-level-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}


	public function actionCheckBrand()
	{
		if($_GET)
		{
			echo UserPositionLevel::model()->checkBrand($_GET['attr_L_id'] , $_GET['attr_M_id']);
		}
	}

}
